package com.sugon.controller.device;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.dao.DevAmTicketItemDao;
import com.sugon.entity.DevAmTicketItemEntity;
import com.sugon.modules.device.model.dto.ModifyLiveImageParam;
import com.sugon.modules.device.service.IDeviceAmTicketManager;
import com.sugon.service.DevAmTicketItemService;
import com.sugon.service.TicketLiveImageService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import com.sugon.utils.ShiroUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 设备自主维护工单项Controller
 *
 * @author YuXD
 * @date 2023-01-23
 */
@Controller
@RequestMapping("devAmTicketItem")
public class DevAmTicketItemController {

    @Resource
    private DevAmTicketItemDao devAmTicketItemDao;
    @Resource
    private DevAmTicketItemService devAmTicketItemService;
    @Resource
    private IDeviceAmTicketManager deviceAmTicketManager;
    @Resource
    private TicketLiveImageService ticketLiveImageService;

    /**
     * 查询临时工单项
     */
    @RequestMapping("/temTicketItems")
    @ResponseBody
    public R temTicketItems(@RequestParam Map<String, Object> params) {
        // 校验工单ID必填
        String ticketId = MapUtil.getStr(params, "ticketId");
        if (StrUtil.isEmpty(ticketId)) {
            return R.error("维护工单必选！");
        }
        //查询列表数据
        Query query = new Query(params);
        List<Map<String, Object>> temTicketItems = devAmTicketItemDao.listTemTicketItems(ticketId);
        PageUtils pageUtil = new PageUtils(temTicketItems, temTicketItems.size(), query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查询计划工单项列表
     */
    @RequestMapping("/planTicketItems")
    @ResponseBody
    public R planTicketItems(@RequestParam Map<String, Object> params) {
        // 校验工单ID必填
        String ticketId = MapUtil.getStr(params, "ticketId");
        if (StrUtil.isEmpty(ticketId)) {
            return R.error("维护工单必选！");
        }
        //查询列表数据
        Query query = new Query(params);
        List<Map<String, Object>> temTicketItems = devAmTicketItemDao.listPlanTicketItems(ticketId);
        PageUtils pageUtil = new PageUtils(temTicketItems, temTicketItems.size(), query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 获取设备维护工单现场图片
     */
    @RequestMapping("/{ticketId}/liveImages")
    @ResponseBody
    public R loadLiveImages(@PathVariable("ticketId") String ticketId) {
        return R.ok().put("liveImageList", ticketLiveImageService.getTicketLiveImages(null, ticketId, "AM"));
    }

    @AutoLog("批量结束设备维护工单项")
    @RequestMapping("/temTicketItems/batchEnd")
    @ResponseBody
    public R batchEnd(@RequestBody Map<String, String[]> params) {
        String[] ticketItemIds = params.get("ids");
        if (ArrayUtil.isNotEmpty(ticketItemIds)) {
            for (String ticketItemId : ticketItemIds) {
                // 校验设备维护项是否存在
                DevAmTicketItemEntity devMaintenanceTicketItem = devAmTicketItemService.queryObject(ticketItemId);
                if (devMaintenanceTicketItem == null) {
                    return R.error("设备维护项不存在!");
                }
                // 校验是否已完成
                if (devMaintenanceTicketItem.getStatus() == 1) {
                    return R.error("当前维护项目已完成!");
                }
                devMaintenanceTicketItem.setImplementerId(ShiroUtils.getUserId());
                deviceAmTicketManager.completeDevMaintenanceOption(devMaintenanceTicketItem);
            }
        }
        return R.ok();
    }

    @NonBusinessParamValid
    @AutoLog("修正现场照片")
    @RequestMapping("/modifyLiveImage")
    @ResponseBody
    public R modifyLiveImage(ModifyLiveImageParam modifyLiveImageParam, BindingResult bindingResult) {
        // 校验设备维护项是否存在
        DevAmTicketItemEntity devAmTicketItem = devAmTicketItemService.queryObject(modifyLiveImageParam.getPmTicketItemId());
        if (devAmTicketItem == null) {
            return R.error("设备维护项不存在!");
        }
        return deviceAmTicketManager.modifyLiveImage(devAmTicketItem, modifyLiveImageParam.getLiveImages()) ? R.ok() : R.error("修正失败，请稍后重试！");
    }

}
